package com.tsd.jxc.req;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsd.core.utils.HlpUtils;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequest;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestDetailExt;
import com.tsd.jxc.fin.entity.JxcFinancePaymentRequestExt;
import com.tsd.jxc.setting.JxcSettingUtil;
import com.tsd.jxc.vo.JxcSettingVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: JxcFinancePaymentRequestReqParamsVo
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/3/18 14:53
 * @Version: 1.0
 */
public class JxcFinancePaymentRequestReqParamsVo extends JxcFinancePaymentRequest {

    private Integer isXd;
    private List<JxcFinancePaymentRequestDetailReqParamsVo> detail_list;

    public Integer getIsXd() {
        return isXd;
    }

    public void setIsXd(Integer isXd) {
        this.isXd = isXd;
    }

    public List<JxcFinancePaymentRequestDetailReqParamsVo> getDetail_list() {
        return detail_list;
    }

    public void setDetail_list(List<JxcFinancePaymentRequestDetailReqParamsVo> detail_list) {
        this.detail_list = detail_list;
    }

    public JxcFinancePaymentRequestExt convert() {
        JxcFinancePaymentRequestExt requestExt = JSON.parseObject(JSON.toJSONString(this), JxcFinancePaymentRequestExt.class);
        List<JxcFinancePaymentRequestDetailExt> detailExtList = requestExt.getDetail_list();
        if (JxcSettingUtil.isClothing()) {
            detailExtList = new ArrayList<>();
            int sn = 1;
            for (JxcFinancePaymentRequestDetailReqParamsVo paramsVo : this.getDetail_list()) {
                Map<String, List<String>> presetDetailMap = paramsVo.getPreset_detail_map();
                if (presetDetailMap == null || presetDetailMap.isEmpty()) {
                    continue;
                }
                for (Map.Entry<String, List<String>> item : presetDetailMap.entrySet()) {
                    List<String> detailList = item.getValue();
                    if (HlpUtils.isEmptyList(detailList)) {
                        continue;
                    }
                    for (String detail : detailList) {
                        JSONObject object = JSON.parseObject(detail);
                        JxcFinancePaymentRequestDetailExt detailExt = new JxcFinancePaymentRequestDetailExt();
                        detailExt.setId(object.getLong("id"));
                        detailExt.setSid(object.getString("sid"));
                        detailExt.setSrc_type(paramsVo.getSrc_type());
                        detailExt.setSrc_sid(object.getString("src_sid"));
                        detailExt.setSrc_parent_sid(object.getString("src_parent_sid"));
                        detailExt.setThe_date(paramsVo.getThe_date());
                        detailExt.setGoods_sid(object.getString("goods_sid"));
                        detailExt.setGoods_name(object.getString("goods_name"));
                        detailExt.setSpec_info(object.getString("spec_info"));
                        detailExt.setUnit(paramsVo.getUnit());
                        detailExt.setQty(object.getBigDecimal("qty"));
                        detailExt.setPrice(object.getBigDecimal("price"));
                        detailExt.setNote_info(paramsVo.getNote_info());
                        detailExt.setSn(sn);
                        detailExtList.add(detailExt);
                        sn += 1;
                    }
                }
            }
        }
        requestExt.setDetail_list(detailExtList);
        return requestExt;
    }

    public static JxcFinancePaymentRequestReqParamsVo transfer(JxcFinancePaymentRequestExt requestExt, JxcSettingVo settingVo) {
        if (requestExt == null) {
            return null;
        }
        JxcFinancePaymentRequestReqParamsVo paramsVo = JSON.parseObject(JSON.toJSONString(requestExt), JxcFinancePaymentRequestReqParamsVo.class);
        List<JxcFinancePaymentRequestDetailExt> detailExtList = requestExt.getDetail_list();
        paramsVo.setDetail_list(JxcFinancePaymentRequestDetailReqParamsVo.transfer(detailExtList, settingVo));
        return paramsVo;
    }
}
